package com.classic.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.classic.base.UBaseApplication;
import com.qy.sdk.Datas.NativeObject;
import com.qy.sdk.Interfaces.RDInterface;
import com.qy.sdk.Utils.NativeModel;
import com.qy.sdk.rds.BannerView;
import com.qy.sdk.rds.NativeView;

import java.util.ArrayList;
import java.util.List;


/**
 * 广告开关工具类
 */
public class ADUtils {
    private static final String DEFAULT_NAME = "AD_switch";
    private static ADUtils sInstance = null;
    private SharedPreferences spf = null;

    private String mSwitch = "switch";//广告开关

    private ADUtils() {
        spf = UBaseApplication.getInstance().getSharedPreferences(DEFAULT_NAME, Context.MODE_PRIVATE);
    }

    public synchronized static ADUtils getInstance() {
        if (sInstance == null) {
            sInstance = new ADUtils();
        }
        return sInstance;
    }

    /**
     * @param isShow 是否默认
     */
    public void setSwitch(boolean isShow) {
        spf.edit().putBoolean(mSwitch, isShow).apply();
    }

    /**
     * @return true 广告开启 false 广告关闭
     */
    public boolean getSwitch() {
        return spf.getBoolean(mSwitch, false);
    }


    /**
     * @return 获取列表iD
     */
    public static void getListAD(FragmentActivity activity, final ADListListener listener) {
        //创建原生实例，原生模板为纯图
        NativeView nativeView = new NativeView(NativeModel.THREE_IMAGE);
//设置广告接口
        nativeView.setInterface(activity, new RDInterface() {
            //获取元素列表
            @Override
            public void onNativeList(List<Object> list) { //实际返回广告数量
                ArrayList<NativeObject> objects = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    NativeObject nativeObject = NativeObject.create(list.get(i));
                    objects.add(nativeObject);
                }
                listener.onSucceed(objects);
            }

            @Override
            public void onError(String string) {
                super.onError(string);
                listener.onError(string);
            }

            @Override
            public void onLoadSuccess() {
                super.onLoadSuccess();
            }

            @Override
            public void onClose() {
                super.onClose();
                listener.onClose();
            }
        });
        nativeView.load(10);
    }

    interface ADListListener {
        void onSucceed(ArrayList<NativeObject> objects);

        void onClose();

        void onError(String error);
    }


    /**
     * 暂时banner广告
     * @param activity
     * @param container
     */
    public static void addADBanner(FragmentActivity activity, final ViewGroup container) {
        Log.d("------qy---------", "banner");
        BannerView ad = new BannerView();
        ad.setInterface(activity, new RDInterface() {
            @Override
            public void rdView(ViewGroup benner) {
                super.rdView(benner);
                container.addView(benner);//layout是你自己定义的布局
                System.out.println("-------qy------- 添加banner：");
            }

            @Override
            public void onError(String string) {
                super.onError(string);

                System.out.println("-------qy------- 添加banner：onError:" + string);
            }

            @Override
            public void onLoadSuccess() {
                super.onLoadSuccess();
                System.out.println("-------qy------- 添加banner：onLoadSuccess");
            }

            @Override
            public void onClose() {
                super.onClose();
                System.out.println("-------qy------- 添加banner：onClose");

            }
        });
        ad.load();
        ad.show();
    }

}